package com.newtype.meeting.server.service;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.newtype.meeting.client.model.MeetingStart;
import com.newtype.meeting.client.service.MeetingStartService;
import com.newtype.meeting.server.PMF;

/**
 * 开始会议业务逻辑实现类
 * @author 汪恭泽
 * Jun 23, 2011 4:02:10 PM
 */
public class MeetingStartServiceImpl extends RemoteServiceServlet implements
		MeetingStartService {
	private static final long serialVersionUID = -6419562289557743962L;

	@Override
	public void saveMeetingStart(MeetingStart meetingStart) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(meetingStart);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MeetingStart> listMeetingStart(String accountID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<MeetingStart> toReturn = new ArrayList<MeetingStart>();
		try {
			Query query = pm
					.newQuery(MeetingStart.class, "notifierID==keyword");
			query.declareParameters("String keyword");
			List<MeetingStart> result = (List<MeetingStart>) query
					.execute(accountID);
			toReturn = (List<MeetingStart>) pm.detachCopyAll(result);
			for (MeetingStart ms : toReturn) {
				ms.updateProperties();
			}
		} finally {
			pm.close();
		}
		return toReturn;
	}

	/** 根据ID删除会议记录 */
	public void deleteMeetingStart(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			MeetingStart toDelete = pm.getObjectById(MeetingStart.class, id);
			pm.deletePersistent(toDelete);
		} finally {
			pm.close();
		}
	}

	/** 根据ID获取会议信息 */
	public MeetingStart getMeetingStart(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			return pm.getObjectById(MeetingStart.class, id);
		} finally {
			pm.close();
		}
	}

	/**
	 *  根据会议类别获取会议集合
	 */
	@SuppressWarnings("unchecked")
	public List<MeetingStart> listMeetingStartByCategory(String categoryID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<MeetingStart> toReturn = new ArrayList<MeetingStart>();
		try {
			Query query = pm.newQuery(MeetingStart.class, "categoryID == keyword");
			query.declareParameters("String keyword");
			List<MeetingStart> result = (List<MeetingStart>) query
					.execute(categoryID);
			toReturn = (List<MeetingStart>) pm.detachCopyAll(result);
			for (MeetingStart start : toReturn) {
				start.updateProperties();
			}
		} finally {
			pm.close();
		}
		return toReturn;
	}
}
